package com.hanson.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.Const;
import com.hanson.common.exception.BusinessException;
import com.hanson.common.service.RedisService;
import com.hanson.entity.GoodsTypeBean;
import com.hanson.entity.IndexNavBean;
import com.hanson.mapper.IndexNavBeanMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@CacheConfig(cacheNames = "NavData")
public class IndexNavService {
    @Autowired
    private IndexNavBeanMapper indexNavBeanMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private GoodsTypeService goodsTypeService;

    @Cacheable(key = "'selectAll'", unless = "#result == null")
    public List<IndexNavBean> selectAll() {
        List<IndexNavBean> list = indexNavBeanMapper.selectAll();
        if (CollectionUtils.isNotEmpty(list)) {
            list.stream().forEach(c -> {
                c.setTypeId(c.getWebTypeId());
            });
        }
        return list;
    }

    private String getTypeIds(List<String> typeIdList) {
        if (CollectionUtils.isEmpty(typeIdList)) {
            throw new BusinessException("商品类型不能为空！");
        }
        return typeIdList.stream()
                .collect(Collectors.joining(","));
    }

    private String getWebTypesId(List<String> typeIdList) {
        List<String> returnList = new ArrayList<>();
        Map<String, Integer> parentMap = new HashMap<>();
        for (String typeId : typeIdList) {
            GoodsTypeBean goodsTypeBean = goodsTypeService.detailGoodsType(Integer.parseInt(typeId));
            if (goodsTypeBean.getPid()==0) {
                parentMap.put(typeId, 0);
                returnList.add(typeId);
            } else {
                if (!parentMap.containsKey(String.valueOf(goodsTypeBean.getPid()))) {
                    returnList.add(typeId);
                }
            }
        }
        return returnList.stream()
                .collect(Collectors.joining(","));
    }

    public void add(IndexNavBean indexNavBean) {
        indexNavBean.setCreateTime(new Date());
        indexNavBean.setTypeId(getTypeIds(indexNavBean.getTypeIds()));
        indexNavBean.setWebTypeId(getWebTypesId(indexNavBean.getTypeIds()));
        indexNavBeanMapper.insert(indexNavBean);
        redisService.del(Const.NAV_REDIS_KEY);
    }

    public void modify(IndexNavBean indexNavBean) {
        IndexNavBean returnBean = indexNavBeanMapper.selectByPrimaryKey(indexNavBean.getId());
        if (null != returnBean) {
            indexNavBean.setUpdateTime(new Date());
            indexNavBean.setTypeId(getTypeIds(indexNavBean.getTypeIds()));
            indexNavBean.setWebTypeId(getWebTypesId(indexNavBean.getTypeIds()));
            indexNavBeanMapper.updateByPrimaryKey(indexNavBean);
            redisService.del(Const.NAV_REDIS_KEY);
        }
    }

    public IndexNavBean detail(Integer id) {
        return indexNavBeanMapper.selectByPrimaryKey(id);
    }

    public void delete(Integer id) {
        indexNavBeanMapper.deleteByPrimaryKey(id);
    }

    public Page<IndexNavBean> listIndexNavs(Page<IndexNavBean> page, IndexNavBean indexNavBean) {
        List<IndexNavBean> list = indexNavBeanMapper.listIndexNavs(page, indexNavBean);
        return page.setRecords(list);
    }
}
